home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / Display Manager Sample / Source / scrolls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-19  |  6.1 KB  |  293 lines  |  [TEXT/CWIE]

  1. /***********************************************************************
  2. #
  3. #        scrolls.c
  4. #
  5. #        This segment handles the scroll bars.
  6. #
  7. #        Author: Michael Marinkovich
  8. #                Apple Developer Technical Support
  9. #
  10. #
  11. #        Modification History: 
  12. #
  13. #            6/4/95        MWM     Initial coding                     
  14. #            10/12/95    MWM        cleaned up
  15. #
  16. #        Copyright © 1992-95 Apple Computer, Inc., All Rights Reserved
  17. #
  18. #
  19. ***********************************************************************/
  20.  
  21. #include <Events.h>
  22. #include <ToolUtils.h>
  23. #include <Gestalt.h>
  24. #include <OSUtils.h>
  25.  
  26.  
  27. #include "App.h"
  28. #include "Proto.h"
  29.  
  30. extern Boolean        gInBackground;
  31.  
  32.  
  33. //----------------------------------------------------------------------
  34. //
  35. //    InstallScrollBars -
  36. //                 
  37. //
  38. //----------------------------------------------------------------------
  39.  
  40. void InstallScrollBars(WindowRef window, DocHnd doc)
  41. {
  42.     Rect        bounds;
  43.     
  44.     
  45.     // calc horiz scroll bar
  46.     bounds = window->portRect;
  47.     
  48.     bounds.right++;
  49.     bounds.left = bounds.right - (kScrollWidth + 1);
  50.     bounds.top--;
  51.     bounds.bottom -= (kScrollWidth - 1);
  52.     
  53.     (**doc).hScroll = NewControl(window, &bounds, nil, true,
  54.                                   0, 0, 0, scrollBarProc, nil);
  55.                                   
  56.     // calc vert scroll bar
  57.     bounds = window->portRect;
  58.  
  59.     bounds.left--;
  60.     bounds.right -= (kScrollWidth - 1);
  61.     bounds.top = bounds.bottom - kScrollWidth;
  62.     bounds.bottom++;
  63.     
  64.  
  65.     (**doc).vScroll = NewControl(window, &bounds, nil, true,
  66.                                   0, 0, 0, scrollBarProc, nil);
  67.  
  68.                                   
  69. }
  70.  
  71.  
  72. //----------------------------------------------------------------------
  73. //
  74. //    AdjustScrollValues -
  75. //                 
  76. //
  77. //----------------------------------------------------------------------
  78.  
  79. void AdjustScrollValues(WindowRef window)
  80. {
  81.     Rect                    contRect;
  82.     Rect                    picFrame;
  83.     ControlHandle            hCntrl;
  84.     ControlHandle            vCntrl;
  85.     short                    oldValue;
  86.     short                    lines;
  87.     short                    max;
  88.     DocHnd                    doc;
  89.     
  90.     doc = (DocHnd)GetWRefCon(window);
  91.     
  92.     if (doc != nil) {
  93.         hCntrl = (**doc).hScroll;
  94.         vCntrl = (**doc).vScroll;
  95.  
  96.         GetContRect(window,&contRect);
  97.         
  98.         if ((**doc).pict != nil)
  99.             picFrame = (**doc).world->portRect;
  100.         else
  101.             SetRect(&picFrame, 0, 0, 0, 0);
  102.             
  103.         oldValue = GetCtlValue(vCntrl);
  104.         lines = picFrame.bottom - picFrame.top;
  105.         max = lines - contRect.bottom ;
  106.         if ( max < 0 ) max = 0;
  107.         SetCtlMax(vCntrl, max);
  108.     
  109.         if(oldValue + contRect.bottom > lines)
  110.             SetCtlValue(vCntrl, max);
  111.         
  112.         oldValue = GetCtlValue(hCntrl);
  113.         lines = picFrame.right - picFrame.left;
  114.         max = lines - contRect.right;
  115.         if ( max < 0 ) max = 0;
  116.         SetCtlMax(hCntrl, max);
  117.     
  118.         if(oldValue + contRect.right > lines)
  119.             SetCtlValue(hCntrl, max);
  120.                 
  121.     }        
  122.             
  123. }
  124.  
  125.  
  126. //----------------------------------------------------------------------
  127. //
  128. //    AdjustScrollbars -
  129. //                 
  130. //
  131. //----------------------------------------------------------------------
  132.  
  133. void AdjustScrollbars(WindowRef window, Boolean resize)
  134. {
  135.     ControlRef        hCtl;
  136.     ControlRef        vCtl;
  137.     Rect            r;
  138.     DocHnd             doc;
  139.  
  140.     
  141.     doc = (DocHnd)GetWRefCon(window);
  142.     
  143.     if (doc != nil && resize) {
  144.         hCtl = (**doc).hScroll;
  145.         vCtl = (**doc).vScroll;
  146.         GetContRect(window, &r);
  147.         
  148.         // resize horizontal
  149.         MoveControl(hCtl, -1, r.bottom);
  150.         SizeControl(hCtl, (r.right - r.left) + 2, kScrollWidth + 1);
  151.         
  152.         // resize vertical
  153.         MoveControl(vCtl, r.right, -1);
  154.         SizeControl(vCtl, kScrollWidth + 1, r.bottom + 2);
  155.     }
  156.     
  157.     // adjust scrollbar values to match
  158.     AdjustScrollValues(window);
  159. }
  160.  
  161.  
  162. //----------------------------------------------------------------------
  163. //
  164. //    ScrollActionProc -
  165. //                 
  166. //
  167. //----------------------------------------------------------------------
  168.  
  169. pascal void ScrollActionProc(ControlRef control, short part)
  170. {
  171.     WindowRef        window;
  172.     short            amount;
  173.     short            value;
  174.     short            max;
  175.     short            hAmount;
  176.     short            vAmount;
  177.     DocHnd            doc;
  178.     
  179.     window = (**control).contrlOwner;
  180.     doc = (DocHnd)GetWRefCon(window);
  181.     
  182.     if (doc != nil) {
  183.         if ( part != 0 ) {                
  184.             window = (*control)->contrlOwner;
  185.             hAmount = vAmount = 0;
  186.             
  187.             value = GetCtlValue(control);    
  188.             max = GetCtlMax(control);        
  189.         
  190.             switch ( part ) {
  191.                 case inUpButton:
  192.                 case inDownButton:        // one line
  193.                         amount = 8;
  194.                         break;
  195.                         
  196.                 case inPageUp:            // one page
  197.                 case inPageDown:
  198.                         amount = 64;
  199.                         break;
  200.             }
  201.             
  202.             if ( (part == inDownButton) || (part == inPageDown) )
  203.                 amount = -amount;        // reverse direction for a downer
  204.             
  205.             amount = value - amount;
  206.             if ( amount < 0 )
  207.                 amount = 0;
  208.             else if ( amount > max )
  209.                 amount = max; 
  210.             SetCtlValue(control, amount);
  211.             
  212.             amount = value - amount;        // calculate the real change
  213.             
  214.             if ( amount != 0 ) {
  215.                 if (control == (**doc).hScroll)
  216.                     hAmount = amount;
  217.                 else
  218.                     vAmount = amount ;
  219.                         
  220.                 MyScrollPicture(window,hAmount, vAmount);
  221.             }
  222.         }    
  223.     }    
  224. }
  225.     
  226.  
  227. //----------------------------------------------------------------------
  228. //
  229. //    MyScrollPicture -
  230. //                 
  231. //
  232. //----------------------------------------------------------------------
  233.  
  234. void MyScrollPicture(WindowRef window, short hs, short vs)
  235. {
  236.     RgnHandle        updateRgn;
  237.     GWorldPtr        oldWorld;
  238.     GWorldPtr        theWorld;
  239.     GDHandle        oldGD;
  240.     PixMapHandle    thePix;
  241.     Rect            contRect;
  242.     Rect            pictRect;
  243.     DocHnd            doc;
  244.     
  245.     doc = (DocHnd)GetWRefCon(window);
  246.     
  247.     if (doc != nil) {
  248.         GetGWorld(&oldWorld, &oldGD);
  249.         SetPort(window);
  250.         
  251.         GetContRect(window, &contRect);
  252.         ScrollRect(&contRect, hs, vs, updateRgn = NewRgn());
  253.         
  254.         theWorld = (**doc).world;
  255.         pictRect = theWorld->portRect;
  256.         
  257.         thePix = GetGWorldPixMap(theWorld);
  258.         if (LockPixels(thePix)) {
  259.             SetGWorld((CGrafPtr)window, nil);
  260.             OffsetRect(&pictRect, -GetCtlValue((**doc).hScroll), 
  261.                       -GetCtlValue((**doc).vScroll));
  262.  
  263.             CopyBits((BitMap *) *thePix, &window->portBits,
  264.                     &theWorld->portRect, &pictRect, srcCopy,updateRgn);
  265.             
  266.             UnlockPixels(thePix);
  267.             
  268.             SetGWorld(oldWorld, oldGD);
  269.         }            
  270.         ClipRect(&window->portRect);
  271.         DisposeRgn(updateRgn);
  272.     }
  273.         
  274. }    
  275.  
  276.  
  277. //----------------------------------------------------------------------
  278. //
  279. //    GetContRect -
  280. //                 
  281. //
  282. //----------------------------------------------------------------------
  283.  
  284. void GetContRect(WindowRef window,Rect *contRect)
  285. {
  286.     *contRect = window->portRect;    // get the content rect for our window
  287.     
  288.     contRect->right -= 15;            // subtract the scroll bars from the rect
  289.     contRect->bottom -= 15;
  290.  
  291.  
  292. }
  293.